package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.dao.TaskReadDao;
import com.ccp.dev.workflow.model.bpm.TaskRead;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;

/**
 * 对象功能:任务是否已读 Service类
 * @author wangtao
 * @date 2019-07-23
 */
@Service
public class TaskReadService extends BaseService<TaskRead>{
	@Resource
	private TaskReadDao dao;

	/**
	 * 添加查看记录
	 * @param procInstId	流程实例ID
	 * @param taskId		任务ID
	 */
	public void saveReadRecord(String procInstId,String taskId){
		SysUser sysUser=(SysUser) ContextUtil.getCurrentUser();
		String userId=sysUser.getUserId();
		if(dao.isTaskRead(taskId, userId)>0){
			return ;
		}

		TaskRead taskRead=new TaskRead();
		taskRead.setId(UUIDUtils.getUUIDFor32());
		taskRead.setProcInstId(procInstId);
		taskRead.setTaskId(taskId);
		taskRead.setUserId(userId);
		taskRead.setUserName(sysUser.getUsername());
		taskRead.setCreateTime(new Date());
		dao.add(taskRead);
	}

	/**
	 * 查看状态是否为已读
	 * @param taskId 流程任务ID
	 * @param userId 用户ID
	 * @return
	 */
	public boolean isRead(String taskId,String userId){
		return dao.isTaskRead(taskId,userId)>0;
	}

	/**
	 * 根据流程实例ID删除任务已读消息
	 * @param procInstId 流程实例ID
	 */
    public void delByProcInstId(String procInstId) {
		dao.delByProcInstId(procInstId);
    }
}
